//
//  GrowthValue.swift
//  demo01
//
//  Created by babybus on 2025/7/7.
//
import Foundation
import UIKit
import SnapKit
import Alamofire

class GrowthValueController: UIViewController{
    //全局变量
    private var topBar:TopBarGVView = TopBarGVView()
    private var taskView:TaskGVView = TaskGVView()
    private var bodyView:BodyGVView = BodyGVView()
    
    private var scrollView = UIScrollView()
    private var contentView = UIView()
    
    override func viewDidLoad(){
        
        self.view.addSubview(scrollView)
        
        
        scrollView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        scrollView.addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.width.equalToSuperview()
        }
        setContentView()
    }
    
    func setContentView(){
        //头部栏
        contentView.addSubview(topBar)
        topBar.snp.makeConstraints { make in
            make.width.equalToSuperview()
            make.height.equalTo(36)
            make.top.equalToSuperview().offset(0)
        }
        //主体信息
        contentView.addSubview(bodyView)
        bodyView.snp.makeConstraints { make in
            make.width.equalToSuperview()
            make.height.equalTo(460)
            make.top.equalTo(topBar.snp.bottom).inset(0)
        }
        
        //任务列表
        contentView.addSubview(taskView)
        taskView.snp.makeConstraints { make in
            make.width.equalToSuperview()
            make.top.equalTo(bodyView.snp.bottom)
//            make.height.equalTo(800)
            make.height.equalTo(taskView.allHeight+30)
            make.bottom.equalToSuperview()
        }
        taskView.delegateView = self
    }
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        // 确保内容大小正确
        scrollView.contentSize = CGSize(width: contentView.frame.size.width, height: 1350)
    }
}

extension GrowthValueController: TaskGVView.ViewDelegate {
    func viewToController(text:String) {
        let vc = TestViewController()
        DispatchQueue.main.async {
            vc.title = text
        }
        // 设置导航跳转至TestNavController页面
        self.navigationController?.pushViewController(vc, animated: true)
    }
    func reLoadPage(){
        updateLayout() // 调用更新布局的方法
    }
    func updateLayout() {
        // 更新 taskView 的高度约束
        taskView.snp.updateConstraints { make in
            make.height.equalTo(taskView.allHeight + 30)
        }
        // 计算 contentView 的总高度
        let totalHeight = topBar.frame.height + bodyView.frame.height + CGFloat(taskView.allHeight) + CGFloat(30)
        scrollView.contentSize = CGSize(width: contentView.frame.width, height: totalHeight)

        // 强制立即布局
        view.setNeedsLayout()
        view.layoutIfNeeded()
    }
}
